//
//  AliPayServer.m
//  CYWFrame
//
//  Created by Macx on 2017/10/14.
//  Copyright © 2017年 Macx. All rights reserved.
//

#import "AliPayServer.h"
#import <AlipaySDK/AlipaySDK.h>
#import "UIView+LZCView.h"
#import <WXApi.h>

// 微信支付回调发送到某个VC
#import "ReplyCommentViewController.h"
#import "MallCommitOrdersViewController.h"
#import "PerWalletViewController.h"
#import "QADetailViewController.h"
#import "MallOrderManageViewController.h"
#import "TabBarViewController.h"
#import "DepositChargeViewController.h"

@implementation AliPayServer

+ (void)wxPay:(NSDictionary *)order {
    
    //构建请求对象
    PayReq *req = [PayReq new];
    req.openID = order[@"appid"];
    req.partnerId = order[@"partnerid"];
    req.prepayId = order[@"prepayid"];
    req.nonceStr = order[@"noncestr"];
//    req.timeStamp = (UInt32)[timeStamp intValue];
    req.timeStamp = (UInt32)[order[@"timestamp"] intValue];
    req.package = order[@"package"];
    req.sign = order[@"sign"];
    //发起微信支付
    BOOL flag = [WXApi sendReq:req];
    
    if (!flag){
        NSLog(@"请求微信失败");
        [LZCTools HUDMessage:@"请安装微信客户端" delay:1.5f];
        return;
    }
    else{
        NSLog(@"请求微信成功");
        
        return;
    }

}

+ (void)aliPay:(NSString *)order memo:(NSString *)memo {
    
    
    // Ali 支付
    NSURL *aliUrl = [NSURL URLWithString:@"alipay:"];
    if (![[UIApplication sharedApplication] canOpenURL:aliUrl]) {
        //这里就是没有安装客户端 网页支付苹果审核会拒绝
        NSLog(@"没有安装");
        [LZCTools HUDMessage:@"请安装支付宝客户端" delay:1.5f];
        return;
    }
    else{
        //这里是已经安装客户端
        NSLog(@"已经安装");
        
    }
    
    UIView *v = [[UIView alloc] init];
    
    UIViewController *currVC = [v topViewControllerWithRootViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
    
    // NOTE: 调用支付结果开始支付
    [[AlipaySDK defaultService] payOrder:order fromScheme:AlipayId callback:^(NSDictionary *resultDic) {
        NSLog(@"支付===reslut = %@",resultDic);
        
        NSDictionary *dic = @{@"pay":@"1"};
        NSString *name;
        
        // 暂时关闭商城，到时完善商城回调
        if ([currVC isKindOfClass:[ReplyCommentViewController class]]) {
            name = @"payReply";
        }
        else if ([currVC isKindOfClass:[PerWalletViewController class]]) {
            name = @"payWallet";
        }
        else if ([currVC isKindOfClass:[MallCommitOrdersViewController class]]) {
            name = @"payMall";
        }
        else if ([currVC isKindOfClass:[QADetailViewController class]]) {
            name = @"payAnswers";
        }
		else if ([currVC isKindOfClass:[DepositChargeViewController class]]) {
			name = @"payDepositCharge";
		}
		else {
			name = @"未定义的viewController";
		}
        NSString *m = resultDic[@"memo"];
        
        if (m.length == 0) {
            m = @"打赏失败";
        }
        
        if (![[NSString stringWithFormat:@"%@", resultDic[@"resultStatus"]] isEqualToString:@"9000"]) {
            
            [currVC presentViewController:[LZCTools alertMessage:m] animated:YES completion:nil];
            //创建通知
            //        NSNotification *notification =[NSNotification notificationWithName:name object:nil userInfo:dic];
            //        //通过通知中心发送通知
            //        [[NSNotificationCenter defaultCenter] postNotification:notification];
        }
        else {
            [currVC presentViewController:[LZCTools alertMessage:@"支付成功"] animated:YES completion:nil];
            //创建通知
            NSNotification *notification =[NSNotification notificationWithName:name object:nil userInfo:dic];
			
            //通过通知中心发送通知
            [[NSNotificationCenter defaultCenter] postNotification:notification];
            
            //根据支付页面回调
            if ([name isEqualToString:@"payMall"]) {
                AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
                appDelegate.window.rootViewController = [[TabBarViewController alloc] init];
                UIView *v = [[UIView alloc] init];
                
                MallOrderManageViewController *order = [[MallOrderManageViewController alloc] init];
                order.defaultIndex = 1;
                UIViewController *currVC = [v topViewControllerWithRootViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
                [currVC.navigationController pushViewController:order animated:YES];
            }
            
        }
        

        
    }];
    
}

@end
